package Utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class HashUtil {

	public static Hashtable<String, Integer> cutHash(Hashtable<String, Integer> ht, int threshold) {
		Hashtable<String, Integer> result = new Hashtable<String, Integer>();
		Entry[] maps = SortUtils.sortByValue(ht, -1);
		for (int i = 0; i < maps.length; i++) {
			if (i> threshold)
				break;
			String key = (String) maps[i].getKey();
			int value = (Integer)maps[i].getValue();
			result.put(key, value);
		}
		return result;
	}

	public static Hashtable<String, Integer> mergeHash(Hashtable<String, Integer> ht1, Hashtable<String, Integer> ht2) {
		Hashtable<String, Integer> ht = new Hashtable<String, Integer>();
		Enumeration<String> en1 = ht1.keys();
		Enumeration<String> en2 = ht2.keys();
		while (en1.hasMoreElements()) {
			String key = (String) en1.nextElement();
			int value = ht1.get(key);
			ht = HashUtil.countHash(ht, key, value);
		}
		while (en2.hasMoreElements()) {
			String key = (String) en2.nextElement();
			int value = ht2.get(key);
			ht = HashUtil.countHash(ht, key, value);
		}
		return ht;
	}

	public static Hashtable<String, Integer> countHash(Hashtable<String, Integer> word_count, String key) {
		if (word_count.containsKey(key))
			word_count.put(key, word_count.get(key) + 1);
		else
			word_count.put(key, 1);
		return word_count;
	}

	public static Hashtable<String, Integer> countHash(Hashtable<String, Integer> word_count, String key, int value) {
		if (word_count.containsKey(key))
			word_count.put(key, word_count.get(key) + value);
		else
			word_count.put(key, value);
		return word_count;
	}

	public static void printHash(Hashtable key_value) {
		Entry[] maps = SortUtils.sortByValue(key_value, -1);
		for(int i=0;i<maps.length;i++)
			System.out.println(maps[i].getKey() + "," + maps[i].getValue());
		System.out.println();
	}

	public static void writeHash(Hashtable ht, String fileName, int order) {
		String content = "";
		Entry[] maps =SortUtils.sortByValue(ht, order);
		for(int i=0;i<maps.length;i++)
		{
			String key = (String) maps[i].getKey();
			int value = (Integer) maps[i].getValue();
			content += key + "#" + value + "\n";
		}

		try {
			File f = new File(fileName);
			if (f.exists()) {
				System.out.println("file existe");
			} else {
				System.out.println("creating ....");
				if (f.createNewFile()) {
					System.out.println("successful created");
				} else {
					System.out.println("fail to created");
				}
			}
			BufferedWriter output = new BufferedWriter(new FileWriter(f));
			output.write(content);
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("finished!");
	}

	public static List<Integer> normalizeHash(List<String> keys, Hashtable<String, Integer> ht) {
		List<Integer> result = new ArrayList<Integer>();
		for (String key : keys) {
			if (ht.containsKey(key))
				result.add(ht.get(key));
			else
				result.add(0);
		}
		System.out.println(result);
		return result;
	}

	public static double[] hashtable2vector(Hashtable<String, Integer> ht) {
		double vector[] = new double[ht.size()];
		Map.Entry[] maps = SortUtils.sortByKey(ht, 1);
		for (int i = 0; i < maps.length; i++) {
			vector[i] = (Integer) maps[i].getValue();
			System.out.println("v:"+(Integer) maps[i].getValue());
		}
		return vector;
	}

	public static void main(String args[]) {
		// Hashtable<String, Integer> ht = new Hashtable<String, Integer>();
		// ht.put("1", 10);
		// ht.put("2", 8);
		// ht.put("4", 5);
		// List<String> keys = new ArrayList<String>();
		// keys.add("1");
		// keys.add("2");
		// keys.add("3");
		// keys.add("4");
		// keys.add("5");
		// normalizeHash(keys, ht);

		// Hashtable<String, Integer> ht1 = new Hashtable<String, Integer>();
		// Hashtable<String, Integer> ht2 = new Hashtable<String, Integer>();
		// ht1.put("1", 1);
		// ht1.put("2", 3);
		// ht1.put("3", 1);
		//
		// ht2.put("1", 2);
		// ht2.put("2", 9);
		// ht2.put("3", 2);
		// ht2.put("4", 1);
		// mergeHash(ht1, ht2);

		Hashtable<String, Integer> ht = new Hashtable<String, Integer>();
		ht.put("3", 10);
		ht.put("2", 3);
		ht.put("1", 12);
		double[] vector = hashtable2vector(ht);
		for (int i = 0; i < vector.length; i++)
			System.out.println(vector[i]);
	}
}
